﻿using Paas.Core.Const.Enums.Approvals;
using Paas.Core.Sys.Entities;
using Basefr.Attributes;
using Basefr.Core.Entities;
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;

namespace Paas.Core.Approvals.Entities
{
    /// <summary>
    /// 审批流程节点
    /// </summary>
    [MainTable(nameof(ApproveStep))]
    public class ApproveStep : EntityBase, ISortable
    {
        /// <summary>
        /// 审批流程
        /// </summary>
        public int ApproveId { get; set; }
        [ForeignKey(nameof(ApproveId))]
        public virtual Approve Approve { get; set; }

        /// <summary>
        /// 模板节点ID
        /// </summary>
        public int? TemplateStepId { get; set; }
        [ForeignKey(nameof(TemplateStepId))]
        public virtual ApproveTemplateStep TemplateStep { get; set; }

        /// <summary>
        /// 节点名称
        /// </summary>
        [MaxLength(100)]
        public string Name { get; set; }

        /// <summary>
        /// 顺序
        /// </summary>
        public int SortIndex { get; set; }

        /// <summary>
        /// 只有流程发起者才有权执行
        /// </summary>
        public bool OperateByInitiator { get; set; }

        /// <summary>
        /// 改变对象状态（按顺序，到下一个状态）
        /// </summary>
        public bool ChangeObjectState { get; set; }

        /// <summary>
        /// 下一步流程节点
        /// </summary>
        public int? NextId { get; set; }
        [ForeignKey(nameof(NextId))]
        public virtual ApproveStep Next { get; set; }

        /// <summary>
        /// 是否当前步骤
        /// </summary>
        public bool IsCurrent { get; set; }

        /// <summary>
        /// 状态
        /// </summary>
        public EnumApproveStepState State { get; set; }

        /// <summary>
        /// 执行人
        /// </summary>
        public long? OperatorId { get; set; }
        [ForeignKey(nameof(OperatorId))]
        public virtual User Operator { get; set; }

        /// <summary>
        /// 执行时间
        /// </summary>
        public DateTime? OperateTime { get; set; }

        /// <summary>
        /// 审批意见
        /// </summary>
        [MaxLength(1000)]
        public string Opinion { get; set; }


        public bool IsLastStep() => NextId == null;

        public bool IsCurrentOperator(long currentUserId) => TemplateStep.Operators.Any(o => o.OperatorId == currentUserId);


    }
}

